package cn.bs.busuo.account.dao.persist.repository.impl;

import cn.bs.busuo.account.dao.persist.mapper.*;
import cn.bs.busuo.account.dao.persist.repository.IUserRepository;
import cn.bs.busuo.account.pojo.cachevo.CacheUserStandardVO;
import cn.bs.busuo.account.pojo.entity.*;
import cn.bs.busuo.account.pojo.param.SetAdminParam;
import cn.bs.busuo.account.pojo.vo.*;
import cn.bs.busuo.common.pojo.vo.PageData;
import cn.bs.busuo.common.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;

import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserRepositoryImpl implements IUserRepository {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public int ifUserExits(Long userId) {
        return userMapper.ifUserExits(userId);
    }

//    @Override
//    public int deleteById(Long id) {
//        return userMapper.deleteById(id);
//    }
//
//    @Override
//    public int deleteById1(Long id) {
//        return userRoleMapper.deleteByUserId(id);
//    }

    @Override
    public int countByUsername(String username) {
        return userMapper.ifExitsByUsername(username);
    }

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public Long selectIdByUsername(String username) {
        return userMapper.selectIDByUsername(username);
    }

    @Override
    public int newUserRole(UserRole userRole) {
        return userRoleMapper.insert(userRole);
    }

    @Override
    public int updateById(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int updatePassword(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public UserStandardVO getStandardById(Long id) {
        return userMapper.getStandardById(id);
    }
    @Override
    public PageData<CacheUserStandardVO> list(Integer pageNum, Integer pageSize) {
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        PageHelper.startPage(pageNum, pageSize);
        List<UserStandardVO> list1 = userMapper.list();
        List<CacheUserStandardVO> list = null;
            for (UserStandardVO userStandardVO : list1) {
                CacheUserStandardVO cacheUserStandardVO = new CacheUserStandardVO();
                BeanUtils.copyProperties(userStandardVO, cacheUserStandardVO);
                String lastLogin = sdf2.format(userStandardVO.getGmtLastLogin());
                cacheUserStandardVO.setGmtLastLogin(lastLogin);
                list.add(cacheUserStandardVO);
            }
        PageInfo<CacheUserStandardVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public String selectUserPasswordById(Long id) {
        return userMapper.selectUserPasswordById(id);
    }

    @Override
    public int selectUserRoleIfExits(SetAdminParam setAdminParam) {
        return userRoleMapper.selectUserRoleIfExits(setAdminParam);
    }

    @Override
    public int setUserRole(UserRole userRole) {
        return userRoleMapper.insert(userRole);
    }

    @Override
    public int deleteUserRole(SetAdminParam setAdminParam) {
        return userRoleMapper.deleteByUserIdAndRoleID(setAdminParam);
    }

    @Override
    public List<AdminUserVO> adminList() {
        return userRoleMapper.adminList();
    }


}
